package com.project.personnel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.personnel.entity.Salary;
import com.project.personnel.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

//薪资设置
@RestController
@RequestMapping("/salary/setting")
public class SalarySettingController {
    @Resource
    private SalaryService salaryService;

    //test
    @GetMapping("/getAll")
    public List<Salary> getAll() {
        return salaryService.list();
    }

    //添加账套
    @PostMapping("/add")
    public boolean add(@RequestBody Salary salary)/*@RequestBody用于添加(post)和修改(put)，前端传来的request body值赋给参数*/ {
        return salaryService.save(salary);
    }

    //批量添加账套
    @PostMapping("/adds")
    public boolean adds(@RequestBody List<Salary> salary) {
        return salaryService.saveBatch(salary);
    }

    //查询编辑账套
    @GetMapping("/getById")
    public Salary getById(@RequestParam("id") Integer id)/*@RequestParam规定参数名对应*/ {
        return salaryService.getById(id);
    }

    //批量查询编辑账套
    @GetMapping("/getByIds")
    public Collection<Salary> getByIds(@RequestParam("id") List<Integer> id) {
        return salaryService.listByIds(id);
    }

    //编辑账套
    @PutMapping("/update")
    public boolean update(@RequestBody Salary salary) {
        return salaryService.updateById(salary);
    }

    //批量编辑账套
    @PutMapping("/updates")
    public boolean updates(@RequestBody List<Salary> salary) {
        return salaryService.updateBatchById(salary);
    }

    //删除
    @DeleteMapping("/delete")
    public boolean delete(@RequestParam("id") Integer id) {
        return salaryService.removeById(id);
    }

    //批量删除
    @DeleteMapping("/deletes")
    public boolean deletes(@RequestParam("id") List<Integer> id) {
        return salaryService.removeByIds(id);
    }

    //查询全部
    @GetMapping("/getSalaryList")
    public List<Salary> getAllList(@RequestParam(defaultValue = "1") Integer current, Integer size) {
        Page<Salary> salaryPage = new Page<>(current, size);
        IPage<Salary> salaryIPage = salaryService.page(salaryPage);
        return salaryIPage.getRecords();
    }
}
